package frontend.components.form;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;

public class SaveDeleteButtonsPanel extends Panel {

    public SaveDeleteButtonsPanel(String id, final ButtonsBehavior behavior) {
        super(id);

        AjaxButton saveButton = new AjaxButton("saveButton") {

            @Override
            public void onConfigure() {
                setVisible(behavior.isSaveButtonVisible());
            }

            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                behavior.onSave(target);
            }

            @Override
            protected void onError(AjaxRequestTarget target, Form<?> form) {
                behavior.onError(target);
            }
        };
        AjaxButton deleteButton = new AjaxButton("deleteButton") {

            @Override
            public void onConfigure() {
                setVisible(behavior.isDeleteButtonVisible());
            }

            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
               behavior.onDelete(target);
            }

            @Override
            protected void onError(AjaxRequestTarget target, Form<?> form) {
               behavior.onError(target);
            }
        };
        deleteButton.setOutputMarkupId(true);
        deleteButton.setOutputMarkupPlaceholderTag(true);

        add(saveButton, deleteButton);
    }
}
